/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package DB;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.Radnik;


/**
 *
 * @author Plan1
 */
public class Baza {
    Connection connection;


    public void otvoriBazu(String url){
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            connection=DriverManager.getConnection(url);
            connection.setAutoCommit(false);
        } catch (SQLException ex) {
            System.out.println("Greska pri uspostavljanju konekcije: "+ex);
        } catch (ClassNotFoundException ex) {
           System.out.println("Greska pri ucitavanju driver-a: "+ex);
        }

    }


    public void zatvoriBazu(){
        try {
            connection.close();
        } catch (SQLException ex) {
            System.out.println("Greska pri zatvaranju baze: "+ex);
        }
    }
    public void commitTransakcije(){
        try {
            connection.commit();
        } catch (SQLException ex) {
            System.out.println("Greska pri komitovanju transakcije: "+ex);
        }
    }
    public void rollbackTransakcije(){
        try {
            connection.rollback();
        } catch (SQLException ex) {
            System.out.println("Greska pri rollback-u transakcije: "+ex);
        }
    }

    public void sacuvajRadnika(Radnik r) {
        try {
            String upit = "INSERT INTO radnik (sifra,ime,prezime,adresa) VALUES('" + r.getSifra() + "','" + r.getIme() + "','" + r.getPrezime() + "','" + r.getAdresa() + "')";
            Statement statement = connection.createStatement();
            statement.execute(upit);
        } catch (SQLException ex) {
            Logger.getLogger(Baza.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

   public ArrayList<Radnik> uzmiSveRadnike() {
        ArrayList<Radnik> lista = new ArrayList<Radnik>();

        try {
            String upit = "SELECT * FROM radnik ";
            Statement statement = connection.createStatement();
            ResultSet rs = statement.executeQuery(upit);

            while(rs.next())
            {
                lista.add(new Radnik(rs.getString("sifra"),rs.getString("ime"), rs.getString("prezime"), rs.getString("adresa")));
            }
        } catch (SQLException ex) {
            Logger.getLogger(Baza.class.getName()).log(Level.SEVERE, null, ex);
        }
       
        return lista;
    }

    public String dajNovuSifru() {

String b="";
        try {
            String upit = "SELECT * FROM radnik ";
            Statement statement = connection.createStatement();
            ResultSet rs = statement.executeQuery(upit);
            while(rs.next()){
             b=rs.getString("sifra");
            }
        } catch (SQLException ex) {
            Logger.getLogger(Baza.class.getName()).log(Level.SEVERE, null, ex);
        }

        return b;
    }

}
